package com.itheima.tanhua.web;

import com.itheima.tanhua.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 1、短信发送验证码
     * @param param
     * @return
     */
    @PostMapping("login")
    public ResponseEntity login(@RequestBody Map<String,String> param){
        boolean flag=userService.sendMsg(param.get("phone"));
        if (!flag){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("验证码还未失效！！");
        }
        return ResponseEntity.ok(null);//200
    }

    /**
     * 2、手机号与验证码登录
     * @param param  手机号：phone   验证码：verificationCode
     * @return
     */
    @PostMapping("loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> param){
        Map map=userService.loginVerification(param);
        return ResponseEntity.ok(map);//[token="",isNew=""]
    }

    /**
     * 向外提供一个校验获取token的接口
     */
    @GetMapping("{token}")
    public Long getToken(@PathVariable("token") String token){
        return userService.getUserToken(token);
    }
}
